<!--
* description: 呆滞件回收价格折让配置
* author: xuliwen
* createdDate: 2021-01-21
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-is-show-more-btn="true"
      :dynamic-api-style="'restful'"
      :callback-obj="callbackObj"
    />

    <!-- <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :dynamic-api-style="'restful'"
      @close="close"
    /> -->
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import OneTableTemplate from "@/components/templates/oneTable";
import { exportExcel } from '@/utils/getTimeString.js'
import Vue from "vue";
import api from '@/api/pa/procuremenData'

export default {
  name: "deadPartsPriceSetting",

  components: {
    OneTableTemplate,
    // Edit: () => import("./edit"),
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ""], function () {
      next();
    });
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path;

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode]);

    return {
      icon: "el-icon-arrow-down",
      status: false,
      editRowData: {},
      selectiondata: [],
      // 网格查询API配置对象
      apiConfig: api.priceStrategyQueryData,
      // 动态组件-按钮
      tableButtons: [ 
        {
          compKey: "btnKey1",
          type: "",
          size: "small",
          // disabled: true,
          parentFileds: "disabled:table",
          clickEvent: () => this.doubleSave(),
          text: "保存", // 新增
          name: "adopt",
          position: "left",
        },
         {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.exportExcel111(),
          text: "导出",
          name: "export",
          position: "left",
        },
      ],


      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
              {
                prop: "setId",
                label: "设置ID",
                hidden: true,
                alwaysHidden: true,
              },
              {
                prop: "updateControlId",
                label: "并发控制ID",
                hidden: true,
                alwaysHidden: true,
              },
              {
                prop: "lookupValueCode",
                label: "备件类别编码",
              },
              {
                prop: "lookupValueName",
                label: "备件类别名称",
              },

              {
                prop: "recycleRatio",
                label: "回收价格比例+",
                isComponent: true,
                comps: [
                  {
                    compKey: "compKey5"+this.$utils.generateId(),
                    disabled: true,
                    codeField: "recycleRatio",
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    rowFileds: 'disabled:discardSetName-是'                 
                  }
                ]
              },
              {
                prop: "remark",
                label: "备注+",
                isComponent: true,
                comps: [
                  {
                    compKey: "compKey5"+this.$utils.generateId(),
                    disabled: true,
                    codeField: "remark",
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    rowFileds: 'disabled:discardSetName-是'                 
                  }
                ]
              },
            ],
      callbackObj: {
        // 手动勾选时
        // onCheckSelect: (selection, row) => {
        //   this.selectiondata = selection;
        // },
        onCheckSelectChange: (selection) => {
          this.selectiondata = selection;
        },
      },
      // 表单查询数据
      formField: this.$utils.getFormField(
        this,
        {
          // discardDate: [moment(new Date()).subtract(1, 'months').format('YYYY-MM-DD'), moment(new Date()).format('YYYY-MM-DD')],
          // lookupTypeCode: "",
          // lookupValueName: "",
          // recycleRatio: "",
          // remark: "",
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      ),
    };
  },
  watch: {
    "formField.orderStatus": {
      deep: true,
      handler: function (newV, oldV) {
        console.log("watch中：", newV);
        // this.querybystatus(newV)
        this.queryTable();
      },
    },
  },
  mounted() {
  },

  methods: {
      exportExcel111(){
      exportExcel(this.formField, api.priceStrategyExportDetails.APIUrl, api.priceStrategyExportDetails.ServiceCode, '呆滞件回收价格折让配置')
      },
      // 保存
 doubleSave() { 
   const that = this
      if(that.selectiondata&&that.selectiondata.length<1){
        that.$message({
            message:'请选择',          
            type: 'warn',
            uration: 1000
          })
      }else{
        that.selectiondata.forEach(element => {
          element.recycleRatio= element.recycleRatio&& element.recycleRatio!=null&&RegExp(/%/).test(element.recycleRatio)?parseFloat(element.recycleRatio):element.recycleRatio        
        });
        const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig:  api.priceStrategySave,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.selectiondata

      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: this.$t('sys.tips.esTip5') /* 保存成功*/,
            type: 'success',
            duration: 1000
          })
          this.queryTable(1)
        } else {
          // 保存失败
          this.$message({
            message:
              '保存失败:' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
      })
      }
      
    },
  },
};
</script>
<style lang='scss' scoped>
</style>